package cn.tedu._05mvcboot01.controller;

import cn.tedu._05mvcboot01.pojo.dto.UserRegDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller注解: 标识当前类为控制器类;
 */

@Controller
public class UserController {
    /**
     * 1.RequestMapping注解：请求注解,根据请求的URL地址映射到控制器方法;
     * 2.ResponseBody注解：响应注解,将控制器方法的返回值作为响应数据;
     */
    @RequestMapping("/login")
    @ResponseBody
    public String login() {
        return "登录成功";
    }

    /**
     * 注册功能:
     * 1.GET请求: http://localhost:8080/reg?username=xxx&password=yyy
     */

    @RequestMapping("/reg")
    @ResponseBody
    public String reg() {
        return "恭喜,注册成功";
    }

    /**
     * 接收用户数据方式1:通过HttpServletRequest对象接收
     * http://localhost:8080/regParam1?username=xxx&password=yyy
     */
    @RequestMapping("/regParam1")
    @ResponseBody
    public String regParam1(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        return username + "---" + password;
    }

    /**
     * 接收用户数据方式2:通过HttpServletRequest对象接收
     * http://localhost:8080/regParam2?username=xxx&password=yyy
     */
    @RequestMapping("/regParam2")
    @ResponseBody
    public String regParam2(String username, String password) {
        return username + "---" + password;
    }

    /**
     * 接收用户数据方式3: 通过POJO类[]接受
     * http://localhost:8080/regParam3?username=xxx&password=yyy
     */
    @RequestMapping("/regParam3")
    @ResponseBody
    public String regParam3(UserRegDTO userRegDTO){
        String username =  userRegDTO.getUsername();
        String password = userRegDTO.getPassword();
        return username + "---" + password;
    }
}
